package com.qingneng.budget.util;





import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.acegisecurity.GrantedAuthority;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.qingneng.budget.security.CurrentUser;
import com.qingneng.budget.security.IUserDetails;
import com.qingneng.budget.vo.Userinfo;

public abstract class BaseAction extends ActionSupport {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 
	 * @param className
	 * @return
	 */
	public Object getBean(String className) {
		
		//ApplicationContext ac = new ClassPathXmlApplicationContext();
		ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(getRequest().getSession().getServletContext());
		return ac.getBean(className);
		 
	}
	
	/**
	 * 
	 * @param className
	 * @return
	 */
	public Object getBean(Class clazz) {
		//ApplicationContext ac = new ClassPathXmlApplicationContext();
		ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(getRequest().getSession().getServletContext());
		return ac.getBean(clazz.getName());
	}

	//得到用户信息
	public Userinfo  getUserInfo(){
		Map session = ActionContext.getContext().getSession();
		Userinfo userInfo = (Userinfo)session.get("userinfo");
		return userInfo;
	}

	//设置用户信息
	public void setUserInfo(Userinfo userInfo){
		Map session = ActionContext.getContext().getSession();
		session.put("userinfo", userInfo);
	}
	
	/**
	 * 判断角色的数据访问权限,如果有多种角色时，默认选择最大级别
	 * ROLE_ADMIN ，ROLE_U_COM_ADMIN，ROLE_U_COM_MANAGER 可访问所有数据-0
	 * ROLE_U_SUBCOM_ADMIN，ROLE_U_SUBCOM_MANAGER 只能访问本公司数据-1
	 * ROLE_USER 只能访问自己录入的数据-2
	 * @return -1-无权限,0-访问所有数据，1-本公司数据，2-本人数据
	 * role ROLE_ADMIN-系统管理员-1，
	 * ROLE_U_COM_ADMIN-总公司管理员-2，
	 * ROLE_U_SUBCOM_ADMIN-分公司管理员-3，
	 * ROLE_U_COM_MANAGER-总公司经理-4，
	 * ROLE_U_SUBCOM_MANAGER-分公司经理-5
	 * * ROLE_USER-普通用户-6，
	 */
	public String volidateAccessDataAuth(){
		String flag = "-1";
		IUserDetails userdetails = CurrentUser.getUser();
		GrantedAuthority[] authorities = userdetails.getAuthorities();
		if(authorities!=null && authorities.length>0){
			for(int i=0;i<authorities.length;i++){
				GrantedAuthority auth = authorities[i];
				String authority = auth.getAuthority();
				if("ROLE_ADMIN".equals(authority)){
					flag = "0";
					break;
				}
				if("ROLE_U_COM_ADMIN".equals(authority)){
					flag = "0";
					break;
				}
				
				if("ROLE_U_COM_MANAGER".equals(authority)){
					flag = "0";
					break;
				}
				
				if("ROLE_U_SUBCOM_ADMIN".equals(authority)){
					flag = "1";
					break;
				}
				if("ROLE_U_SUBCOM_MANAGER".equals(authority)){
					flag = "1";
					break;
				}
				if("ROLE_USER".equals(authority)){
					flag = "2";
					break;
				}
			}
		}
		
		return flag;
	}
	

	protected void processPaginationBean() {
		String t_row = getRequest().getParameter("rows");
		if(t_row==null || "".equals(t_row)){
			t_row = "10";
		}
		String t_page = getRequest().getParameter("page");
		if(t_page==null || "".equals(t_page)){
			t_page = "1";
		}
		int rows = Integer.parseInt(t_row);
		int page = Integer.parseInt(t_page);	
		PaginationBean paginationBean = new PaginationBean();
		paginationBean.setPageNo(page);
		paginationBean.setPageSize(rows);
		paginationBean.setStartIndex(paginationBean.getStart());
		PaginationContext.setPaginationBean(paginationBean);
	}
	
	/**
	 * 获得分页PaginationBean的方法
	 * @return
	 */
	protected PaginationBean getPaginationBean(){
		return PaginationContext.getPaginationBean();
	}

	/**
	 * 清除分页PaginationBean的方法
	 */
	protected void clearPaginationBean(){
		PaginationContext.clearPaginationBean();
	}
	
	/**
	 * 取得HttpRequest的简化方法.
	 */
	public static HttpServletRequest getRequest()
	{
		return ServletActionContext.getRequest();
	}
}